import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.AirlineReservationServiceProxy;
import bean.Person;
import constants.AirlineReservationServiceConstants;


public class AuthenticatePersonServlet extends HttpServlet{

	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		AirlineReservationServiceProxy proxy = new AirlineReservationServiceProxy(
				AirlineReservationServiceConstants.SERVICE_URL);
			
		String username = req.getParameter("userName").trim();
		String password = req.getParameter("password").trim();
		
		Person person = proxy.authenticateUser(username, password);
		
		System.out.println("Inside AuthenticateUserServlet....."+ person);
		
		if(person!=null && person.getUserName()!=null){
			HttpSession session = req.getSession();
			req.getSession().setAttribute("user", person);
			if(person.getType().equalsIgnoreCase("employee") || person.getType().equalsIgnoreCase("admin")){
				req.getRequestDispatcher("employee/employeeStart.jsp").forward(req, resp);
			}else if(person.getType().equalsIgnoreCase("customer")){
				req.getRequestDispatcher("customer/customerStart.jsp").forward(req, resp);
			}
					
			
		}else{
			
			req.setAttribute("Error", "Incorrect Username/password");
			req.getRequestDispatcher("signin.jsp").forward(req, resp);
			
			
		}
		
		//req.getRequestDispatcher("employee/employeeStart.jsp").forward(req, resp);
		
		
	}
}
